
console.log('Demo Run JS.[8899]..');

$('video').attr('autoplay', 'autoplay');

window.checkVideoTotal = 0;

function checkVideo(){
    
    window.checkVideoTotal++;
    
    if ($('video').length != 0) {
        
        var video = $('video')[0];
        
        console.log('已经获取到Video', video.duration, video);
        
        if (isNaN(video.duration)) {
            if (window.checkVideoTotal > 30) {
                window.location.reload();
            } else {
                setTimeout(function(){
                    checkVideo();
                }, 3000);
            }
        } else {
            
            //video.play();
            
            checkTimeout(video.duration);
            
        }
    } else {
        if (window.checkVideoTotal > 10) {
            window.location.reload();
        } else {
            setTimeout(function(){
                checkVideo();
            }, 3000);
        }
    }
}


function checkTimeout(time) {
    
    if (time <= 0) {
        
        console.log('计时结束,准备下一课吧...');
        
        setTimeout(function(){
            console.log('top.postMessage()...');
            top.postMessage('hello,autoPlay().', '*');
        }, 10000);
        
    } else {
        
        //video.play();
        
        console.log('video倒计时：' + time + ' [' + (time / 60) + ']');
        
        setTimeout(function(){
            checkTimeout(time-5);
        }, 5000);
        
    }
    
}

setTimeout(function(){
    checkVideo();
}, 5000);


